package com.example.imtoken.modules.main.my.address.create.select

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BusUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.ActivitySelectAddressTypeBinding
import com.example.imtoken.entitys.http.CoinsEntity

class SelectAddressTypeActivity :
    BaseActivity<SelectAddressTypeViewModel, ActivitySelectAddressTypeBinding>(
        SelectAddressTypeViewModel::class.java
    ) {

    private lateinit var adapter: SelectAddressTypeAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_select_address_type
    }

    override fun init() {
        BusUtils.register(this)
        initTitle()
        initClick()
        initAdapter()
        initObserve()
    }

    private fun initObserve() {
        viewModel.getCoinsListData().observe(this) {
            adapter.setList(it)
        }
    }

    @BusUtils.Bus(tag = BusConfig.out_data_sticky_select_address_type_activity, sticky = true)
    fun getAddressType(map: HashMap<String, Any>) {
        val data = map["data"] as CoinsEntity
        val p = map["p"] as Int
        data[p].isAddToken = 1
        viewModel.setCoinsListData(data)
    }

    private fun initAdapter() {
        adapter = SelectAddressTypeAdapter(R.layout.item_select_address_type)
        val layoutManager = LinearLayoutManager(this)
        dataBinding.rvAddressType.layoutManager = layoutManager
        dataBinding.rvAddressType.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            setResult(200, intent.putExtra("p", position))
            this.finish()
        }
    }

    private fun initClick() {
        dataBinding.include6.ivHeadBack.setOnClickListener {
            this.finish()
        }
    }

    private fun initTitle() {
        dataBinding.include6.tvHeadTitles.text = getString(R.string.addressType)
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.out_data_sticky_select_address_type_activity)
        BusUtils.unregister(this)
    }

}